import React, {Component} from 'react'
import {ListBox} from '../../../components'
import {fetchData} from '../../../components/AjaxConsumer'
import {Modal} from 'antd';
import AddPage from './addPage'
import UpdatePage from './UpdatePage'
import PrintInfo from './printInfo'
import Info from './info'

export default class ListProHandInPage extends Component {
    constructor(props) {
        super(props);
        //行操作列表
        this.rowOperation = [
        ];
        //header 操作列表
        this.headerOperation = [
            {
                handleName: 'add',
                showName: '新增',
                iconClass: 'plus',
                permission: 'handin-add'
            },
            {
                handleName: 'edit',
                showName: '修改',
                iconClass: 'edit',
                permission: 'handin-edit'
            }
            //,
            //{
            //    handleName: 'print',
            //    showName: '出库打印',
            //    iconClass: 'printer',
            //    permission: 'prodbout-outprint'
            //},
            //{
            //    handleName: 'export',
            //    showName: 'Excel',
            //    iconClass: 'file-excel'
            //}

        ];
        //table列表展示列配置
        //storageKey 本地存储的key
        //showName、columnName 必有属性， columnWidth 可选，默认100, columnHidden: true 列隐藏（用于列配置显示隐藏）, foreverHidden: true 列永久隐藏
        this.tableColumn = {
            storageKey: 'handInId',
            data: [
                {
                    showName: 'handInId',
                    columnName: 'handInId',
                    columnHidden:true,
                    foreverHidden: true
                },
                {
                    showName: '入库单号',
                    columnName: 'handInNo',
                    columnWidth: 110
                },{
                    showName: '合同编号',
                    columnName: 'contractNo'
                },{
                    showName: '型号',
                    columnName: 'model'
                },{
                    showName: '型号描述',
                    columnName: 'modelDesc'
                },{
                    showName: '型号类型',
                    columnName: 'modelType'
                },{
                    showName: '单位',
                    columnName: 'unit',
                    columnWidth: 100
                },{
                    showName: '需求数量',
                    columnName: 'requireAmount',
                    columnWidth: 100
                },{
                    showName: '实际数量',
                    columnName: 'realAmount',
                    columnWidth: 100
                }
                //,{
                //    showName: '入库原因',
                //    columnName: 'reason'
                //}
                ,{
                    showName: '任务状态',
                    columnName: 'handInStatus',
                    showOption:[
                        {
                            optionKey:'0',
                            optionShow:'新建'
                        },{
                            optionKey:'1',
                            optionShow:'入库中'
                        },{
                            optionKey:'2',
                            optionShow:'入库完成'
                        }
                    ]
                },{
                    showName: '创建日期',
                    columnName: 'gmtCreate',
                    columnWidth: 130
                }
            ]
        };
        //搜索属性配置，如果字段不配置，默认是input搜索
        // 搜索类型：
        //      1、输入框 type='input'， 默认
        //      2、时间 type='date'
        //      3、数字 type='number'
        //      4、下拉框 type='select'
        //      5、下拉框 type='disabled'
        this.searchComponentData = [
            {
                showName: '型号类型',
                name: 'modelType',
                type: 'select',
                optionList: [//下拉框的信息
                ]
            },
            {
                name:'gmtCreate',
                type: 'date',
                maxName:'gmtCreateMax',
                minName:'gmtCreateMin',
                showTime:'1'
            },{
                showName: '需求数量',
                name:'requireAmount',
                type: 'number',
                maxName:'requireAmountMax', //最大值
                minName:'requireAmountMin'  //最小值
            },{
                showName: '实际数量',
                name:'realAmount',
                type: 'number',
                maxName:'realAmountMax', //最大值
                minName:'realAmountMin'  //最小值
            },{
                showName: '任务状态',
                name:'handInStatus',
                type: 'select',
                optionList:[//下拉框的信息
                    {
                        optionKey:'',
                        optionShow:''
                    },
                    {
                        optionKey:'0',
                        optionShow:'新建'
                    },{
                        optionKey:'1',
                        optionShow:'入库中'
                    },{
                        optionKey:'2',
                        optionShow:'入库完成'
                    }
                ]
            }
        ];

    }

    rowHandles = (strHandles, rowObj) => {
        if(strHandles === 'add'){
            this.addHandle(rowObj);
        }else if(strHandles == 'edit'){
            this.editHandle(rowObj);
        }else if(strHandles == 'doubleClick'){
            this.infoHandle(rowObj);
        }else if (strHandles == 'print') {
            this.printHandle(rowObj);
        }else if (strHandles == 'export') {
            this.exportXlsHandle(rowObj);
        }
    };

    //按照库位新增
    addHandle = () => {
        this.refs.addPage.addHandle();
    };

    editHandle = (rowObj) => {
        if(rowObj.length == 0){
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
        }else if(rowObj.length > 1){
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
        }else{
            this.refs.updatePage.editHandle(rowObj);
        }
    };

    infoHandle = (rowObj) => {
        this.refs.info.editHandle(rowObj[0].data);
    };

    printHandle = (rowObj) => {
        if(rowObj.length == 0){
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
        }else if(rowObj.length > 1){
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
        }else {
            this.refs.printInfo.printHandle(rowObj);
        }
    }

    exportXlsHandle =(conditonData) =>{
        console.log('-------导出条件--------',conditonData);
        let param ='?'
        for(let key in conditonData){
            if(conditonData[key]!=''){
                console.log(key,conditonData[key]);
                param = param + key + '=' + conditonData[key] + '&'
            }
        }
        console.log(param);
        window.location.href='/wmsrest/product/odsprodbout/list/exp'+param;
    }

    //刷新列表
    refreshHandle = () => {
        this.refs.listOdsproorderin.refreshHandle();
    };

    componentWillMount() {
        const dicType = 'model_type_p';
        const options= {
            url: '/base/dic/get/' + dicType,
            type: 'get',
            condition:{}
        };
        //先写死，后面再改
        fetchData(this, options, this.dicInitCallBackHandle);
    }

    //下拉框 初始化
    dicInitCallBackHandle = (resData)=>{
        //alert('resData' + JSON.stringify(resData, null, 4));
        let optionList = [];
        let tmp = {};
        tmp.optionKey = '';
        tmp.optionShow = '';
        optionList.push(tmp);
        resData.data.map( (e,index) => {
            let tmp = {};
            tmp.optionKey = e.dicCode;
            tmp.optionShow = e.dicName;
            optionList.push(tmp);
        });

        //alert('optionList' + JSON.stringify(optionList, null, 4));
        this.tableColumn.data[5].showOption = optionList;
        this.searchComponentData[0].optionList = optionList;
    }

    render() {
        //当左侧隐藏时，列表宽度加上左侧的宽度，
        //1、当前this.props.isHideLeft 是从页面组件containers/Main/main.js传过来
        //2、this.props.isHideLeft 最顶级赋值是在页面 containers/Main/main.js
        let isHideLeft = this.props.isHideLeft;

        return(
            <div>
                <ListBox
                    ref = 'listOdsproorderin'
                    title="手工入库任务查询"
                    isHideLeft={isHideLeft}
                    tableColumnData = {this.tableColumn}
                    searchComponentData={this.searchComponentData}
                    url = '/product/intowarehouse/prohandin/list'
                    rowHandles = {this.rowHandles}
                    rowOperation = {this.rowOperation}
                    headerOperation = {this.headerOperation}
                    isRowSingle={true}
                />
                <AddPage
                    ref="addPage"
                    refreshHandle = {this.refreshHandle}
                />
                <UpdatePage
                    ref="updatePage"
                    refreshHandle = {this.refreshHandle}
                />
                <Info
                    ref="info"
                />
                <PrintInfo
                    ref="printInfo"
                    refreshHandle = {this.refreshHandle}
                />
            </div>
        )
    }
}
